package se.gtg.einstein;

import java.util.ArrayList;

public class SearchLists {
	private ArrayList<SearchList> list = new ArrayList<SearchList>();
	private int currentSearchList = -1;
	
	public void add(SearchList searchList){
		list.add(searchList);
	}
	
	public ArrayList<SearchList> getSearchListsArray(){
		return list;
	}
	
	public void remove(SearchList searchList){
		list.remove(searchList);
	}

	public boolean hasDoublets() {
		for(SearchList searchList : list) {
			if (searchList.hasDoublets()) return true;
		}
		
		return false;
	}
	
	public void clear(){
		list.clear();
	}

	public SearchList getCurrentSearchList() {
		 return list.get(currentSearchList);
	}

	public void setCurrentSearchList(SearchList searchList) {
		 list.set(currentSearchList, searchList);
	}

	public SearchList nextSearchListWithDoublets() {
		return getNextSearchListWithDoublets(true);
	}
	
	public boolean hasMoreSearchListWithDoublets() {
		return getNextSearchListWithDoublets(false) != null;
	}


	private SearchList getNextSearchListWithDoublets(boolean addCurrent) {
		int i = currentSearchList;
		i++;
		SearchList searchList = null;
		
		for (;i < list.size() ; i++){
			searchList = list.get(i);
			if (searchList.hasDoublets()){
				break;
			}
			
			searchList = null;
		}
		
		if (addCurrent == true) currentSearchList = i;
		return searchList;
	}
}
